package com.toweave.starter.components;

import static com.toweave.starter.utils.ConstantsUtil.LIZ_TAG;

import android.content.Context;
import android.text.Editable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.toweave.starter.R;

import java.util.Objects;

public class AsEditText extends LinearLayout {
  private TextView tv_as_edit_name;
  private ClearableEditText cet_as_edit_value;

  public AsEditText(Context context, AttributeSet attrs) {
    super(context, attrs);
    init(context, attrs);
  }

  public AsEditText(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    init(context, attrs);
  }

  private void init(Context context, AttributeSet attrs) {
    LayoutInflater.from(context).inflate(R.layout.as_edit_text, this);
    tv_as_edit_name = findViewById(R.id.tv_as_edit_name);
    cet_as_edit_value = findViewById(R.id.cet_as_edit_value);
    String labelPosition = cet_as_edit_value.getLabelPosition();
    Log.i(LIZ_TAG, "labelPosition ->: " + labelPosition);

    // 设置引用组件的事件监听
    cet_as_edit_value.setOnEditListener(new OnEditAdapter() {
      // @Override
      // public void onChange(Editable s) {
      //   Log.i(LIZ_TAG, "onChange s ->: " + s);
      // }
      @Override
      public void onClear() {
        Log.i(LIZ_TAG, "onClear ->: ");
        String value = Objects.requireNonNull(cet_as_edit_value.getText()).toString();
        Log.i(LIZ_TAG, "onClear ->: " + value);
      }
    });
    tv_as_edit_name.setText("用户名");
  }

  // 添加一个方法来移除内部ClearableEditText的监听器
  public void removeClearableEditTextListener() {
    if (cet_as_edit_value != null) {
      cet_as_edit_value.removeOnEditListener();
    }
  }
  // end
}
